import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from sklearn import neighbors,datasets

# 加载数据
input_file = 'F:/python学习资料/Python-Machine-Learning-Cookbook-master/Chapter05/data_nn_classifier.txt'

def load_data(input_file):
    X = []
    y = []
    with open(input_file,'r') as f:
        for line in f.readlines():
            data = [float(x) for x in line.split(",")]
            X.append(data[:-1])
            y.append(data[-1])

    X = np.array(X)
    y = np.array(y).astype(int)
    return X,y


X,y = load_data(input_file=input_file)
print(X[:5])
print(y[:5])

# 画出输入数据
plt.figure()
plt.title("Input datapoints")
markers = '^sov<>hp'
mapper = np.array([markers[i] for i in y])
for i in range(X.shape[0]):
    plt.scatter(X[i,0],X[i,1],marker=mapper[i],s=50,edgecolors='black',facecolors='none')



# 考虑最近邻的个数
num_neighbors = 10
# 网格步长
h = 0.01

# 创建Knn分类器模型并训练
classifier = neighbors.KNeighborsClassifier(num_neighbors,weights='distance')
classifier.fit(X,y)
# 生成网格并画出边界
x_min,x_max = X[:,0].min(),X[:,0].max()
y_min,y_max = X[:,1].min(),X[:,1].max()
x_grid,y_grid = np.meshgrid(np.arange(x_min,x_max,h),np.arange(y_min,y_max,h))
# 计算网格所有点的输出
predicted_values = classifier.predict(np.c_[x_grid.ravel(),y_grid.ravel()])
# 画出计算结果
predicted_values = predicted_values.reshape(x_grid.shape)
plt.figure()
plt.pcolormesh(x_grid,y_grid,predicted_values,cmap=cm.Paired)
# 在图上画出训练数据点
for i in range(X.shape[0]):
    plt.scatter(X[i,0],X[i,1],marker=mapper[i],s=50,edgecolors='black',facecolors='none')

plt.xlim(x_grid.min(),x_grid.max())
plt.ylim(y_grid.min(),y_grid.max())
# 接下来测试一个输入点
test_data = [4.5,3.6]
for i in range(X.shape[0]):
    plt.scatter(X[i,0],X[i,1],marker=mapper[i],s=50,edgecolors='black',facecolors='none')
plt.scatter(test_data[0],test_data[1],marker='x',s=200,facecolors='black')
print(classifier.predict([test_data]))
plt.show()
distance,indices = classifier.kneighbors([test_data])
print(distance)
print(indices)
